package com.swak.vertx.invoker;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import com.swak.exception.BlockException;

/**
 * 禁止调用阻塞的方式获取数据
 * 
 * @author lifeng
 * @date 2021/03/22
 */
public class ForbidBlockCompletableFuture<T> extends CompletableFuture<T> {

    @Override
    public T get() throws InterruptedException, ExecutionException {
        if (this.isCancelled() || this.isDone()) {
            return super.get();
        }
        throw new BlockException("Flux 调用栈中不能调用 get() 来阻塞当前的线程！");
    }

    @Override
    public T get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
        if (this.isCancelled() || this.isDone()) {
            return super.get(timeout, unit);
        }
        throw new BlockException("Flux 调用栈中不能调用 get(long, TimeUnit) 来阻塞当前的线程！");
    }

    @Override
    public T join() {
        if (this.isCancelled() || this.isDone()) {
            return super.join();
        }
        throw new BlockException("Flux 调用栈中不能调用 join() 来阻塞当前的线程！");
    }
}
